﻿using System;
using System.Collections.Generic;
using RimWorld;
using Verse;
using Verse.AI;

namespace RI_Living
{
	// Token: 0x02000005 RID: 5
	public class JobDriver_TakeCocoonOutOfRI_Building_SilkwormRearingBox : JobDriver
	{
		// Token: 0x1700000B RID: 11
		// (get) Token: 0x06000023 RID: 35 RVA: 0x0000291C File Offset: 0x00000B1C
		protected Building_Fermentation Barrel
		{
			get
			{
				return (Building_Fermentation)this.job.GetTarget(TargetIndex.A).Thing;
			}
		}

		// Token: 0x1700000C RID: 12
		// (get) Token: 0x06000024 RID: 36 RVA: 0x00002948 File Offset: 0x00000B48
		protected Thing Thing
		{
			get
			{
				return this.job.GetTarget(TargetIndex.B).Thing;
			}
		}

		// Token: 0x06000025 RID: 37 RVA: 0x00002970 File Offset: 0x00000B70
		public override bool TryMakePreToilReservations(bool errorOnFailed)
		{
			return this.pawn.Reserve(this.Barrel, this.job, 1, -1, null, errorOnFailed, false);
		}

		// Token: 0x06000026 RID: 38 RVA: 0x000029A3 File Offset: 0x00000BA3
		protected override IEnumerable<Toil> MakeNewToils()
		{
			this.FailOnDespawnedNullOrForbidden(TargetIndex.A);
			this.FailOnBurningImmobile(TargetIndex.A);
			yield return Toils_Goto.GotoThing(TargetIndex.A, PathEndMode.Touch, false);
			yield return Toils_General.Wait(200, TargetIndex.None).FailOnDestroyedNullOrForbidden(TargetIndex.A).FailOnCannotTouch(TargetIndex.A, PathEndMode.Touch).FailOn(() => !this.Barrel.Fermented).WithProgressBarToilDelay(TargetIndex.A, false, -0.5f);
			Toil toil = ToilMaker.MakeToil("MakeNewToils");
			toil.initAction = delegate()
			{
				Thing thing = this.Barrel.TakeOutBeer();
				GenPlace.TryPlaceThing(thing, this.pawn.Position, base.Map, ThingPlaceMode.Near, null, null, default(Rot4));
				StoragePriority currentPriority = StoreUtility.CurrentStoragePriorityOf(thing);
				IntVec3 c;
				bool flag = StoreUtility.TryFindBestBetterStoreCellFor(thing, this.pawn, base.Map, currentPriority, this.pawn.Faction, out c, true);
				if (flag)
				{
					this.job.SetTarget(TargetIndex.C, c);
					this.job.SetTarget(TargetIndex.B, thing);
					this.job.count = thing.stackCount;
				}
				else
				{
					base.EndJobWith(JobCondition.Incompletable);
				}
			};
			toil.defaultCompleteMode = ToilCompleteMode.Instant;
			yield return toil;
			yield return Toils_Reserve.Reserve(TargetIndex.B, 1, -1, null, false);
			yield return Toils_Reserve.Reserve(TargetIndex.C, 1, -1, null, false);
			yield return Toils_Goto.GotoThing(TargetIndex.B, PathEndMode.ClosestTouch, false);
			yield return Toils_Haul.StartCarryThing(TargetIndex.B, false, false, false, true, false);
			Toil carryToCell = Toils_Haul.CarryHauledThingToCell(TargetIndex.C, PathEndMode.ClosestTouch);
			yield return carryToCell;
			yield return Toils_Haul.PlaceHauledThingInCell(TargetIndex.C, carryToCell, true, false);
			yield break;
		}

		// Token: 0x04000014 RID: 20
		private const TargetIndex BarrelInd = TargetIndex.A;

		// Token: 0x04000015 RID: 21
		private const TargetIndex BeerToHaulInd = TargetIndex.B;

		// Token: 0x04000016 RID: 22
		private const TargetIndex StorageCellInd = TargetIndex.C;

		// Token: 0x04000017 RID: 23
		private const int Duration = 200;
	}
}
